#include "key.h"
#include "config.h"
#include "wifi.h"
#include "led_display.h"
#include "sub_device.h"
#include "music.h"

#define RIGHT_KEY_PORT           GPIOB
#define RIGHT_KEY_PORT_PIN            GPIO_PIN_5

#define LEFT_KEY_PORT_PORT                      GPIOB
#define LEFT_KEY_PORT_PIN                       GPIO_PIN_7

#define MIDDLE_KEY_PORT_PORT      GPIOB  
#define MIDDLE_KEY_PORT_PIN       GPIO_PIN_6

#define KEY_DOWN_FLAG         1
#define KEY_UP_FLAG           0

#define LEFT_FLAG_BIT     0
#define RIGHT_FLAG_BIT       1
#define MIDDLE_FLAG_BIT       2

#define KEY_SHAKE_THR         4
#define KEY_PAIR_THR      300
#define KEY_ADD_SUBDEV_THR      300
#define KEY_CLEAN_PAIR_THR      800
#define KEY_AP_SMART_THR      600
#define KEY_SMARTCONFIG_THR      600
#define KEY_FACTORY_RESET_THR      1000

#define NO_KEY_EVENT          0
#define CHANGE_MUSIC_NEXT_EVENT    1
#define CHANGE_MUSIC_PRE_EVENT    10
#define VOL_UP_EVENT          2
#define SMARTCONFIG_EVENT     3
#define AP_EVENT              4
#define ADD_DEV_EVENT         5
#define CANCLE_ADD_DEV_EVENT  6
#define GATEWAY_PAIR_EVENT    7
#define GATEWAY_UNPAIR_EVENT    8
#define GATEWAY_FACTORY_EVENT   9

 
static uint8_t key_press_flag;
static uint8_t key_event;

static volatile uint32_t key_timer_cnt;
static volatile uint32_t factory_key_timer_cnt; 

void key_timer_int_handler(void)
{
   if(key_press_flag)
   key_timer_cnt++;

   factory_key_timer_cnt++;
}

void key_init(void)
{
   rcu_periph_clock_enable(RCU_GPIOA);
   rcu_periph_clock_enable(RCU_GPIOB);
   /* configure button pin as input */
   gpio_mode_set(RIGHT_KEY_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, RIGHT_KEY_PORT_PIN);
   gpio_mode_set(LEFT_KEY_PORT_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, LEFT_KEY_PORT_PIN);
   gpio_mode_set(MIDDLE_KEY_PORT_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, MIDDLE_KEY_PORT_PIN); 
   
}

void key_mainloop(void)
{
   static uint8_t flag, flag1; 
   uint8_t n;
   if((gpio_input_bit_get(RIGHT_KEY_PORT, RIGHT_KEY_PORT_PIN)==0)&&\
      (gpio_input_bit_get(LEFT_KEY_PORT_PORT, LEFT_KEY_PORT_PIN)==0)&&\
      (gpio_input_bit_get(MIDDLE_KEY_PORT_PORT, MIDDLE_KEY_PORT_PIN)==0))
   {
      if(flag1==0)
      {
         factory_key_timer_cnt=0;
         flag1 = 1;
      }
      if(factory_key_timer_cnt>=3000)
      {
         flag1 = 0;
         factory_key_timer_cnt=0;
         factory_all();
         factory_all_led();
      }
      key_press_flag = 0;
      key_timer_cnt = 0;
      key_event = NO_KEY_EVENT;
     
      return;
   }
   if(gpio_input_bit_get(RIGHT_KEY_PORT, RIGHT_KEY_PORT_PIN)==0)
   {
      if(key_press_flag>>LEFT_FLAG_BIT&0x1||key_press_flag>>MIDDLE_FLAG_BIT&0x1)
      {
         //key was using
         return;
      }
      
      key_press_flag |= 1<<RIGHT_FLAG_BIT;
      if((key_timer_cnt>=KEY_SHAKE_THR)&&(key_timer_cnt<KEY_ADD_SUBDEV_THR))
      {
         key_event = CHANGE_MUSIC_NEXT_EVENT;         
      }
      else if((key_timer_cnt>=KEY_ADD_SUBDEV_THR)&&(key_timer_cnt<KEY_SMARTCONFIG_THR))
      {
         
         if(key_event!=ADD_DEV_EVENT)
         {
            if(mcu_get_wifi_work_state()==WIFI_CONN_CLOUD)
            {
                set_red_display_mode(ADD_SUBDEV_MODE);
            }
            
            gd_printf(("will be go in add subdev\r\n"));
         }
         
         key_event = ADD_DEV_EVENT;
      }
      else if((key_timer_cnt>=KEY_SMARTCONFIG_THR)&&(key_timer_cnt<KEY_FACTORY_RESET_THR))
      {
         
            if(key_event!=SMARTCONFIG_EVENT)
            {
               set_red_display_mode(PROMPT_SMARTCONFIG_MODE);
               gd_printf(("will be go in smartconfig mode\r\n"));
            }
            key_event = SMARTCONFIG_EVENT;            
         
      }
      else if(key_timer_cnt>=KEY_FACTORY_RESET_THR)
      {
         if(key_event!=GATEWAY_FACTORY_EVENT)
         {
            if(mcu_get_wifi_work_state()!=WIFI_CONN_CLOUD)
            {
               factory_reset_by_key();
               
               key_event = GATEWAY_FACTORY_EVENT;
               return;
            }
            gd_printf(("gateway reset facotry success\r\n"));
            set_red_display_mode(PRE_GATEWAY_RESET_FACTORY_MODE);
            reset_factory_setting();
            
            gd_printf((" go in reset factory mode\r\n"));
         }
         
         key_event = GATEWAY_FACTORY_EVENT;
      }
      
   }
   else if(gpio_input_bit_get(LEFT_KEY_PORT_PORT, LEFT_KEY_PORT_PIN)==0)
   {
      if(key_press_flag>>RIGHT_FLAG_BIT&0x1||key_press_flag>>MIDDLE_FLAG_BIT&0x1)
      {
         //vol down key was using
         return;
      }
      key_press_flag |= 1<<LEFT_FLAG_BIT;
    

      if((key_timer_cnt>=KEY_SHAKE_THR)&&(key_timer_cnt<KEY_PAIR_THR))
      {
         key_event = VOL_UP_EVENT;
         
      }
      else if((key_timer_cnt>=KEY_PAIR_THR)&&(key_timer_cnt<KEY_CLEAN_PAIR_THR))
      {
         if(key_event!=GATEWAY_PAIR_EVENT)
         {
            set_blue_display_mode(GATEWAY_PAIR_MODE);
         }
         key_event = GATEWAY_PAIR_EVENT;
      }
      else if(key_timer_cnt>=KEY_CLEAN_PAIR_THR)
      {
         if(key_event!=GATEWAY_UNPAIR_EVENT)
         {
            gd_printf(("gateway unpair effect\r\n"));
            clean_gateway_pair_button();
            
            music_play_item(MUSIC_CLEANED_ALL_PAIR);
            set_blue_display_mode(GATEWAY_UNPAIR_MODE);
            ack_gateway_all_info();
         }
         
         key_event = GATEWAY_UNPAIR_EVENT;
      }
   }
   else if(gpio_input_bit_get(MIDDLE_KEY_PORT_PORT, MIDDLE_KEY_PORT_PIN)==0)
   {
      if(key_press_flag>>LEFT_FLAG_BIT&0x1||key_press_flag>>RIGHT_FLAG_BIT&0x1)
      {
         //vol down key was using
         return;
      }
      key_press_flag |= 1<<MIDDLE_FLAG_BIT;
      if((key_timer_cnt>=KEY_SHAKE_THR)&&(key_timer_cnt<KEY_AP_SMART_THR))
      {
         key_event = CHANGE_MUSIC_PRE_EVENT;
      }
      else if(key_timer_cnt>=KEY_AP_SMART_THR)
      {
         if(key_event!=AP_EVENT)
         {
            set_red_display_mode(PROMPT_AP_MODE);
            gd_printf(("will be go in ap mode\r\n"));
         }
         key_event = AP_EVENT;
      }
   }
   else
   {  
      if((key_event!=NO_KEY_EVENT)&&(flag==0))
      {
         key_timer_cnt = 0;
         flag = 1;
      }
      if(flag==1)
      {
         if(key_timer_cnt<3)
         return;
      }

      if(key_event!=NO_KEY_EVENT)
      {
          if(cancle_add_subdev())
          {
              key_event = NO_KEY_EVENT;
              //music_play_item(MUSIC_SUBDEV_CANCLE_ADD_SUBDEV);
              return;
          }
          if(gateway.pair_flag==1)
          {
             if(key_event==VOL_UP_EVENT)
             {
                cancle_gateway_pair();
                key_event = NO_KEY_EVENT;
              return;
             }
          }
          
      }
      
      switch(key_event)
      {
         case NO_KEY_EVENT:
              
         break;
         case CHANGE_MUSIC_NEXT_EVENT:
         case CHANGE_MUSIC_PRE_EVENT: 
         //testtttt();
              if(key_event==CHANGE_MUSIC_NEXT_EVENT)
              {
                 if(gateway.pair_flag||gateway.pair_button_cnt==1)
                 {
                    gateway.pair_music++;
                    if(gateway.pair_music>MUSIC_CHANG_LING)
                    gateway.pair_music = MUSIC_BIAO_ZHUN;
                    music_play_item(gateway.pair_music);
                    if(gateway.pair_button_cnt==1)
                    {
                        
                        n = find_index_when_pair_button_only_one();
                        
                        gateway.u.flash.pair_button[n].music_item = gateway.pair_music;
                        record_gateway_in_flash();
                        //ack_gateway_pair_to_cloud(n);
                    }
                 }
                 else
                 {
                    gateway.play_music++;
                    if(gateway.play_music>MUSIC_CHANG_LING)
                    gateway.play_music = MUSIC_BIAO_ZHUN;
                    music_play_item(gateway.play_music);
                    
                 }
              }
              else
              {
                 if(gateway.pair_flag||gateway.pair_button_cnt==1)
                 {
                 gateway.pair_music--;
                 if(gateway.pair_music<MUSIC_BIAO_ZHUN)
                 gateway.pair_music = MUSIC_CHANG_LING;
                 music_play_item(gateway.pair_music);
                  if(gateway.pair_button_cnt==1)
                    {
                        n = find_index_when_pair_button_only_one();
                        
                        gateway.u.flash.pair_button[n].music_item = gateway.pair_music;
                        record_gateway_in_flash();
                       // ack_gateway_pair_to_cloud(n);
                    }
                 }
                 else
                 {
                 gateway.play_music--;
                 if(gateway.play_music<MUSIC_BIAO_ZHUN)
                 gateway.play_music = MUSIC_CHANG_LING;
                 music_play_item(gateway.play_music);
                 }
                 
                 
                 
              }
              
              
              gd_printf(("change music effect,pair_music=%d,play_music=%d\r\n",gateway.pair_music,gateway.play_music));
              
              set_blue_display_mode(MUSIC_CHANGE_MODE);
              //reset_factory_setting();
         break;
         case VOL_UP_EVENT:    
              if(gateway.pair_flag==1)
              {
                 gateway.pair_flag=0;
                 gateway.pair_filter=0;
                 music_play_item(MUSIC_PAIR_TIMEOUT);
                 set_blue_display_mode(BLUE_LED_IDLE);
                 break;
              }
              music_set_vol(++gateway.u.flash.vol,1);
              mcu_dp_enum_update(30, gateway.u.flash.vol, my_strlen(GATEWAY_ID), GATEWAY_ID);
              
         break;
         case SMARTCONFIG_EVENT:
              gateway.first_connected=0;
              mcu_set_wifi_mode(SMART_CONFIG);
              gd_printf(("go in smartconfig effect\r\n"));
         break;
         case AP_EVENT:
              gateway.first_connected=0;
              mcu_set_wifi_mode(AP_CONFIG);
              gd_printf(("go in ap effect\r\n"));
         break;
         case ADD_DEV_EVENT:
              gd_printf(("add sub device effect\r\n"));
              if(mcu_get_wifi_work_state()==WIFI_CONN_CLOUD)
              {
                  if(search_bind_flag==NO_SEARCH_BIND)
                  {
                      search_bind_flag = WAIT_ADDSUB_ACK_FROM_CLOUD_BY_KEY;
                      local_add_subdev_limit(1, 0, 30);
                  }
              }
              else
              {
                  music_play_item(MUSIC_ADD_SUBDEV_FAIL_OFFLINE);
                  set_red_display_mode(RED_LED_IDLE);
                  gd_printf(("net is not conneted,please connect dev to cloud first\r\n"));
              }
              
         break;
         case CANCLE_ADD_DEV_EVENT:
              gd_printf(("cancle add sub device effect\r\n"));
         break;
         case GATEWAY_UNPAIR_EVENT:
              
         break;
         case GATEWAY_PAIR_EVENT:
              
              if(is_gateway_pair_button_full()==0)
              {
                 music_play_item(MUSIC_PAIR_BUTTON_EXCCED);
                 gd_printf(("gateway pair button full\r\n"));
                 break;
              }
              gd_printf(("gateway pair effect\r\n"));
              music_play_item(MUSIC_PRESS_DOORBELL_BUTTON);
              gateway.pair_flag = 1;
              gateway.pair_filter=3000;
         break;
      }
      key_press_flag = 0;
      key_timer_cnt = 0;
      key_event = NO_KEY_EVENT;
      flag = 0;
      flag1 = 0;
      factory_key_timer_cnt=0;
   }

   
}
